package controller;
import java.io.IOException;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOException;
import model.PublicHolidayDTO;
import bizlogic.PublicHolidayManager;
/**
 * Servlet implementation class PublicHolidayupdateServlet
 * @param <DTOholiday>
 */

public class PublicHolidayupdateServlet<DTOholiday> extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PublicHolidayupdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws SQLException, ParseException, DAOException {
		DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT);
	PublicHolidayManager PhManager = new PublicHolidayManager();
	PublicHolidayDTO DTOholiday = new PublicHolidayDTO();
	
	DTOholiday.setName(request.getParameter("publicholidayname"));
	String hDate = request.getParameter("publicholidaydate");
	
	Date sDate = formatter.parse(hDate);
	
	java.sql.Date mysqlsDate = new java.sql.Date(sDate.getTime());
	
	DTOholiday.setDate(mysqlsDate);

	String ins = (String) request.getParameter("ins");
	Logger.getLogger(getClass().getName()).log(Level.INFO,
			"Insert Flag: " + ins);
	if (ins.equalsIgnoreCase("true")) {
		PhManager.addPublicHoliday(DTOholiday);
	} else {
		PhManager.updatePublicHoliday(DTOholiday);
		}
	
	ArrayList<PublicHolidayDTO> data= PhManager.findAllPublicHoliday();
	request.setAttribute("holiday", data);
	RequestDispatcher rd = request.getRequestDispatcher("/PublicHolidayListing.jsp");//"/leaveappforiss/EmployeeListPage.jsp"
	try {
		rd.forward(request, response);
	} catch (ServletException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

}
